package cn.itcast.mq;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Description SpringRabbitListener
 * @Author junjie Wu 吴俊杰
 * @Date 2024-03-19
 */
@Component
public class SpringRabbitListener {

    /**
     * 基本队列
     * @param msg
     */
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg) {
//        System.out.println("spring 消费者接收" + msg);
//    }

    /**
     * 工作队列消费者1
     * @param msg
     */
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue1Message(String msg) throws InterruptedException {
//        System.out.println("工作队列消费者1" + msg);
//        Thread.sleep(25);
//    }
//
//    /**
//     * 工作队列消费者2
//     * @param msg
//     */
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue2Message(String msg) throws InterruptedException {
//        System.err.println("工作队列消费者2" + msg);
//        Thread.sleep(40);
//    }


    /**
     * 广播模式
     * @param msg
     * @throws InterruptedException
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutMessage(String msg) throws InterruptedException {
        System.out.println("消费者接收到广播消息 fanout.queue1 " + msg);
    }

    /**
     * 广播模式
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanout2Message(String msg) throws InterruptedException {
        System.err.println("消费者接收到广播消息 fanout.queue2" + msg);
    }

//    /**
//     * 路由模式
//     * @param msg
//     * @throws InterruptedException
//     */
//    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1"),exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
//    key = {"blue","red"}))
//    public void listenDirectQueue1Message(String msg) throws InterruptedException {
//        System.out.println("路由模式消费者接收到消息 direct.queue1 " + msg);
//    }
//
//    /**
//     * 路由模式
//     * @param msg
//     */
//    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2"),
//                  exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT), key = {"red","yellow"}))
//    public void listenDirectQueue2Message(String msg) throws InterruptedException {
//        System.err.println("路由模式消费者接收到消息 direct.queue2" + msg);
//    }


    /**
     * 主题模式
     * @param msg
     * @throws InterruptedException
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic123",type = ExchangeTypes.TOPIC),
            key = "china.#"))
    public void listenTopicQueue1Message(String msg) throws InterruptedException {
        System.out.println("路由模式消费者接收到消息 topic.queue1 " + msg);
    }

    /**
     * 主题模式
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic123",type = ExchangeTypes.TOPIC), key = "#.news"))
    public void listenTopicQueue2Message(String msg) throws InterruptedException {
        System.err.println("路由模式消费者接收到消息 topic.queue2" + msg);
    }

    /**
     * 接收object类型消息
     * @param map
     */
    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String,Object> map) {
        System.out.println("接收object类型消息");
        System.out.println(map);
    }
}
